package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.logger.LogMessageType;
import com.conversationboard.model.Log;
import com.conversationboard.model.User;

@WebServlet(name = "AdminClearLogServlet", urlPatterns = "/Admin/AdminClearLogServlet")
public class AdminClearLogServlet extends HttpServlet {

	private static final long serialVersionUID = 1063721091276181796L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		try {

			User user = User.get(request.getUserPrincipal().getName());

			String type = request.getParameter("type");

			if (user.isSiteAdmin() && SecurityChecks.passes(request, user, this)) {
				Log.clear(LogMessageType.valueOf(type));
			}

			response.sendRedirect(request.getHeader("referer"));

		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
